//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.Partner;
import com.wsd.smartcarwasher.service.MachineService;
import com.wsd.smartcarwasher.service.PartnerService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(
        tags = {"管理端-运营商相关接口"}
)
@Controller
@CrossOrigin
public class PartnerController {
    private static final Logger logger = LoggerFactory.getLogger(MachineService.class);
    @Resource
    private PartnerService partnerService;
    @Resource
    private WxService wxService;
    @Resource
    private MachineService machineService;

    public PartnerController() {
    }
    @Log("新建加盟商")
    @RequiresPermissions("create:new:partner")
    @ApiOperation(
            value = "新建加盟商",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partner"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap addPartner(@RequestBody @ApiParam Partner partner){
        if (partner != null && partner.isFieldsValid()) {
            if (this.partnerService.hasPartnerWithCellphone(partner.getCellphone())) {
                return ReturnUtil.Error(ResultState.PHONE_USED.getMsg());
            } else {
                int result = this.partnerService.addPartner(partner);
                if (result == 0) {
                    return ReturnUtil.Error(ResultState.ADD_PARTNER_FAIL.getMsg());
                } else {
                    partner.setSalt((String)null);//盐值不返回前端
                    return ReturnUtil.Success(ResultState.ADD_PARTNER_SUCCESS.getMsg(), partner);
                }
            }
        } else {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
    }




    @ApiOperation(
            value = "获取所有加盟商信息",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partners"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap queryUsersWithRole() {
        //return ReturnUtil.Success(ResultState.QUERY_PARTNER_SUCCESS.getMsg(), this.partnerService.queryAllPartner());
        return ReturnUtil.Success(ResultState.QUERY_PARTNER_SUCCESS.getMsg(), this.partnerService.queryAllPartnerCount());
    }

    @ApiOperation(
            value = "绑定运营商",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "cellphone",
            value = "电话号码",
            required = true,
            dataType = "String"
    ), @ApiImplicitParam(
            name = "password",
            value = "密码",
            required = true,
            dataType = "String"
    )})
    @RequestMapping(
            value = {"/v1/relate_partner"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap relatePartner(@RequestParam("cellphone") String cellphone, @RequestParam("password") String password, HttpServletRequest request) {
        if (!StringUtils.isBlank(cellphone) && !StringUtils.isBlank(password)) {
    String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
            return this.partnerService.relatePartner(openId, cellphone, password);
        } else {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
    }
    @Log("修改加盟商")
    @RequiresPermissions("update:partner")
    @ApiOperation(
            value = "更新加盟商",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "partner",
            value = "加盟商信息",
            required = true,
            dataType = "Partner"
    )
    @RequestMapping(
            value = {"/v1/update_partner"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap updatePartner(@RequestBody Partner partner) {
        Partner oldPartner = this.partnerService.getPartnerById(partner.getId());
        if (oldPartner == null) {
            return ReturnUtil.Error(ResultState.PARTNER_NOT_EXIST.getMsg());
        } else {
            int changeRows = this.partnerService.updatePartner(partner);
            return changeRows == 0 ? ReturnUtil.Error(ResultState.UPDATE_PARTNER_FAIL.getMsg()) : ReturnUtil.Success(ResultState.UPDATE_PARTNER_SUCCESS.getMsg());
        }
    }
    @Log("删除加盟商")
    @RequiresPermissions("delete:partner")
    @ApiOperation(
            value = "删除加盟商",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "id",
            value = "加盟商id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/delete_partner"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap delatePartner(@RequestParam("id") int partnerId) {
        Partner oldPartner = this.partnerService.getPartnerById(partnerId);
        if (oldPartner == null) {
            return ReturnUtil.Error(ResultState.PARTNER_NOT_EXIST.getMsg());
        } else {
            int changeRows = this.partnerService.deletePartnerId(partnerId);
            if (changeRows == 0) {
                return ReturnUtil.Error(ResultState.UPDATE_PARTNER_FAIL.getMsg());
            } else {
                int changes = this.machineService.removeMachineOfPartner(partnerId);
                logger.info("removeMachineOfPartner, partnerId=" + partnerId + "changeRows = " + changes);
                return ReturnUtil.Success(ResultState.UPDATE_PARTNER_SUCCESS.getMsg());
            }
        }
    }
    @ApiOperation(
            value = "获取加盟商下的所有设备",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @RequestMapping(
            value = {"/v1/partners/getMachines"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap getMachines(HttpServletRequest request) {

String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if (StringUtils.isBlank(openId)) {
            return ReturnUtil.Error(ResultState.LOGIN_AGAIN.getMsg());
        } else {
            Partner partner = this.partnerService.getPartnerByOpenId(openId);
            if (partner == null) {
                return ReturnUtil.Error(ResultState.NO_BIND_PARTNER.getMsg());
            } else {
                if(partner.getIs_dataer()==1)
                {
                    return ReturnUtil.Success(ResultState.QUERY_PARTNER_SUCCESS.getMsg(), this.partnerService.getMachines(partner.getMajor_account()));

                }
                //return ReturnUtil.Success(ResultState.QUERY_PARTNER_SUCCESS.getMsg(), this.partnerService.queryAllPartner());
                return ReturnUtil.Success(ResultState.QUERY_PARTNER_SUCCESS.getMsg(), this.partnerService.getMachines(partner.getId()));

            }

        }

    }
    //2020/9/5测试  在商家端添加自己的数据员
@ApiOperation(
        value = "2020/9/5测试 商家端添加自己的数据员",
        notes = "数据员管理",
        produces = "application/json",
        response = ModelMap.class
)
@RequestMapping(
        value = {"/v1/partners/addPartnerDataer"},
        method = {RequestMethod.POST},
        produces = {"application/json; charset=utf-8"}
)
@ResponseBody
public ModelMap addPartnerDataer(HttpServletRequest request,@RequestBody @ApiParam Partner partner) {

            if (partner != null && partner.isFieldsValid()) {
                partner.setIs_dataer(1);
                partner.setIs_super(0);

                if (this.partnerService.hasPartnerWithCellphone(partner.getCellphone())) {
                    return ReturnUtil.Error(ResultState.PHONE_USED.getMsg());
                } else {
                    int result = this.partnerService.addPartner(partner);
                    if (result == 0) {
                        return ReturnUtil.Error(ResultState.ADD_PARTNER_FAIL.getMsg());
                    } else {
                        partner.setSalt((String)null);//盐值不返回前端
                        return ReturnUtil.Success(ResultState.ADD_PARTNER_SUCCESS.getMsg(), partner);
                    }
                }
            } else {
                return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
            }
        }




    @ApiOperation(
            value = "删除加盟商数据员",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "id",
            value = "加盟商id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/partners/delete_dataer"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap delateDataPartner(@RequestParam("id") int partnerId) {
        Partner oldPartner = this.partnerService.getPartnerById(partnerId);
        if (oldPartner == null) {
            return ReturnUtil.Error(ResultState.PARTNER_NOT_EXIST.getMsg());
        } else {
            int changeRows = this.partnerService.deletePartnerId(partnerId);
            if (changeRows == 0) {
                return ReturnUtil.Error(ResultState.UPDATE_PARTNER_FAIL.getMsg());
            } else {
                int changes = this.machineService.removeMachineOfPartner(partnerId);
                logger.info("removePartnerDataer, partnerId=" + partnerId + "changeRows = " + changes);
                return ReturnUtil.Success(ResultState.UPDATE_PARTNER_SUCCESS.getMsg());
            }
        }
    }


    /**
     * 运营端退出登录
     * @param request
     */
    @GetMapping(value = "/v1/wx/loginout")
    @ResponseBody
    public ModelMap partnerLoginOUT(HttpServletRequest request) throws IOException {
String sessionId = request.getHeader("authorization");
        String openId = this.wxService.getOpenId(sessionId);
//String openId="o1MBY6MMA3D1NM_WuA8OuuwrgxrM";
        if(openId!=null)
        {
            Partner partner = this.partnerService.getPartnerByOpenId(openId);
            this.partnerService.delPartnerOpenId(partner.getId());
        }
        return  ReturnUtil.Success("成功");
    }




    //查看自己的数据员
    @ApiOperation(
            value = "查看全部的数据员",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "id",
            value = "加盟商id",
            required = true,
            dataType = "Integer"
    )
    @RequestMapping(
            value = {"/v1/partners/get_dataer"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap get_dataer(HttpServletRequest request,@RequestParam(value = "id") String partnerId) {
                List<Partner> dataer=this.partnerService.getDataerParList(partnerId);
                return  ReturnUtil.Success("查询成功",dataer);
    }


    @Log("修改数据源")
    @ApiOperation(
            value = "更新数据员信息",
            notes = "账号管理",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "partner",
            value = "加盟商信息",
            required = true,
            dataType = "Partner"
    )
    @RequestMapping(
            value = {"/v1/partners/update_partner"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    @ResponseBody
    public ModelMap updateDataPartner(@RequestBody Partner partner) {
        Partner oldPartner = this.partnerService.getPartnerById(partner.getId());
        if (oldPartner == null) {
            return ReturnUtil.Error(ResultState.PARTNER_NOT_EXIST.getMsg());
        } else {
            int changeRows = this.partnerService.updatePartner(partner);
            return changeRows == 0 ? ReturnUtil.Error(ResultState.UPDATE_PARTNER_FAIL.getMsg()) : ReturnUtil.Success(ResultState.UPDATE_PARTNER_SUCCESS.getMsg());
        }
    }







}
